<?php

declare (strict_types=1);
/**
 * @copyright 深圳市易果网络科技有限公司
 * @version 1.0.0
 * @link https://dayiguo.com
 */

namespace App\Service\Store;

use App\Dao\AgentDao;
use App\Dao\BusinessDao;
use App\Dao\ScanOrderDao;

use App\Dao\ScanOrderRefundsDao;
use App\Dao\UserDao;
use App\Exception\CustomException;
use App\Job\BusinessMoneyJob;
use App\Job\IntegralJob;
use App\Job\ScanOrderTimeOutJob;
use App\Kernel\Util\UserJwtInstance;
use App\Service\Service;
use App\Middleware\User\StoreMiddleware;

use App\Service\User\IntegralDetailService;
use App\Service\User\MoneyService;
use Hyperf\DbConnection\Db;
use Hyperf\Snowflake\IdGeneratorInterface;
use Hyperf\Di\Annotation\Inject;

/**
 * 线下扫码订单服务
 *
 * @author 张昊(2396675557@qq.com)
 * @package App\Service\User
 */
class ScanOrderService  extends Service
{

    /**
     * @Inject
     * @var MoneyService
     */
    public $user_money_service;

    /**
     * @Inject
     * @var ScanOrderDao
     */
    public $scan_order_dao;

    /**
     * @Inject
     * @var ScanOrderRefundsDao
     */
    public $scan_order_refunds_dao;

    /**
     * @Inject
     * @var UserDao
     */
    public $user_dao;

    /**
     * @Inject
     * @var AgentDao
     */
    public $agent_dao;

    /**
     * @Inject
     * @var \App\Service\Agent\MoneyService
     */
    private $agent_monet_service;

    /**
     * @Inject
     * @var IntegralDetailService
     */
    private $integral_detail_service;

    /**
     * 扫码订单列表
     * @param array $map
     * @return mixed
     */
    public function list(array $map)
    {
        $result = $this->scan_order_dao->get($map);

        $data = [];
        if ($result) {
            foreach ($result as $key => $value) {
                $data['list'][$key]['id'] = $value['id'];
                $data['list'][$key]['user_id'] = $value['user_id'];
                $data['list'][$key]['order_no'] = $value['order_no'];
                $data['list'][$key]['created_at'] = date('Y.m.d', $value['created_at']);
                $data['list'][$key]['amount'] = $value['amount'];
                $data['list'][$key]['business_id'] = $value['business_id'];
                $data['list'][$key]['status'] = $value['status'];
                $data['list'][$key]['business_name'] = $value['business']['name'];
                $data['list'][$key]['phone'] = $value['business']['phone'];
                $data['list'][$key]['tel'] = $value['business']['tel'];

                $scanOrderRefunds = $this->scan_order_refunds_dao->first([
                    'order_no' => $value['order_no']
                ]);

                if ($scanOrderRefunds) {
                    $data['list'][$key]['title'] = $scanOrderRefunds['title'];
                    $data['list'][$key]['refunds_created_at'] = date('Y.m.d', $scanOrderRefunds['created_at']);
                } else {
                    $data['list'][$key]['title'] = '';
                    $data['list'][$key]['refunds_created_at'] = '';
                }
            }
        }
        $data['count'] = $this->scan_order_dao->count($map);
        return $data;
    }

    /**
     * 删除线下分润
     * @param string $order_no
     * @return mixed
     */
    public function refunds(string $order_no)
    {
        /**
         * 删除线下分润，必须是已经付款的，同时没有删除过的 ， 需要删除的东西有商家分润 渠道商分润 各级代理 积分
         */
        $scan_order = $this->scan_order_dao->first([
            'order_no' => $order_no
        ]);

        if( $scan_order['status'] != 1 ) {
            $this->error();
        }

        $user_money = $this->user_money_service->money_dao->get([
            'order_no' => $order_no
        ],false);

        if( $user_money ) { //商家 渠道商
            foreach ($user_money as $key => $value) {
                $user = $this->user_dao->first($value['user_id']);
                if( $user['money'] >= $value['num'] ) {
                    $this->user_money_service->money_dao->delete($value['id']);
                    if( empty($value['business_id']) ) {
                        $this->user_money_service->updateMoneyUserBalance($user['id']);
                    } else {
                        $this->user_money_service->updateMoneyBalance($value['business_id']);
                    }
                }
            }
        }

        $agent_money = $this->user_money_service->agent_money_dao->get([
            'order_no' => $order_no
        ],false);

        if( $agent_money ) {// 代理
            foreach ($agent_money as $key => $value) {
                 $agent = $this->agent_dao->first($value['agent_id']);
                 if( $agent['money'] >=  $value['num']) {
                     $this->user_money_service->agent_money_dao->delete($value['id']);
                     $this->agent_monet_service->updateMoneyAgentBalance($agent['id']);
                 }
            }
        }

        $integral = $this->integral_detail_service->integral_detail_dao->getIntegralDetailsMap([
            'order_no' => $order_no
        ]);

        if( $integral ) {
            foreach ($integral as $key => $value) {
                $user = $this->user_dao->first($value['user_id']);

                if( $user['integral'] >= $value['num'] ) {
                    $this->integral_detail_service->integral_detail_dao->delete($value['id']);
                    $this->driver->push(new IntegralJob( $value['user_id'] ));
                }
            }
        }

        $this->user_money_service->updateMoneyUserBalance($scan_order['user_id']);
        $this->user_money_service->updateMoneyBalance($scan_order['business_id']);
    }

}